• Jump To … +
    main.js separate.js single.js web-apg-api.js main.js web-conv-api.js ast.js csv.js dangling-else.js display.js flags.js float.js limits.js main.js multiline-mode.js recursive.js replace.js rules.js split.js testonly.js trace.js udt.js unicode.js web-email.js word-boundaries.js main.js phone-number.js web-main.js web-phone-number.js main.js phone-number.js setup.js translate.js xml.js branch-fail-grammar.js main.js parent-mode-grammar.js setup.js universal-mode-grammar.js colors-app.js colors-callbacks.js colors.js main.js more-app.js more-setup.js more.js ast-callbacks.js bad-input.js basic.js ini-file.js main.js parser-callbacks.js setup.js trace.js anbncn.js and.js c-comment.js compound.js main.js nested.js not.js setup.js boundaries-grammar.js boundaries.js comment-grammar.js comment.js main.js negative-grammar.js negative.js positive-grammar.js positive.js setup.js main.js odata-grammar.js run.js setup.js area-code.js lookaround.js main.js phone-number.js setup.js simple.js all-operators.js default.js fancy-number.js limited-lines.js main.js select-operators.js select-rules.js setup.js main.js minimal.js parent-u.js parent.js phone-number.js setup.js stats.js trace.js universal-u.js universal.js callbacks.js grammar.js main.js parser.js writeHtml.js LICENSE.md README.md index.md
  • ast-callbacks.js

  • §
    /* eslint-disable func-names */
    /* eslint-disable no-restricted-syntax */
    /*  *************************************************************************************
     *   copyright: Copyright (c) 2021 Lowell D. Thomas, all rights reserved
     *     license: BSD-2-Clause (https://opensource.org/licenses/BSD-2-Clause)
     *   ********************************************************************************* */
  • §

    This module defines the AST translation callback functions functions. This is where the real work is done manipulating the data gleaned from the INI file format.

    module.exports = function astCallbacks() {
      const { apgLib } = require('apg-js');
      const id = apgLib.ids;
      let currentSection;
      let currentKey;
  • §

    Find a specific named key in a section object.

      const findKey = function (keyname, section) {
        const lower = keyname.toLowerCase();
        for (const name in section) {
          if (name.toLowerCase() === lower) {
            return section[keyname];
          }
        }
        return undefined;
      };
  • §

    Find a specific named section in a list of sections.

      const findSection = function (sectionname, sections) {
        const lower = sectionname.toLowerCase();
        for (const name in sections) {
          if (name.toLowerCase() === lower) {
            return sections[sectionname];
          }
        }
        return undefined;
      };
  • §

    Initialize the collection data object.

      function astIniFile(state, chars, phraseIndex, phraseLength, data) {
        const ret = id.SEM_OK;
        if (state === id.SEM_PRE) {
          currentSection = {};
          data[0] = currentSection;
          currentKey = undefined;
        }
        return ret;
      }
  • §

    Collect key name and as set it the current key object.

      function astKeyName(state, chars, phraseIndex, phraseLength) {
        const ret = id.SEM_OK;
        if (state === id.SEM_PRE) {
          const name = apgLib.utils.charsToString(chars, phraseIndex, phraseLength);
          currentKey = findKey(name, currentSection);
          if (currentKey === undefined) {
            currentKey = [];
            currentSection[name] = currentKey;
          }
        }
        return ret;
      }
  • §

    Collect section name and as set it the current section object.

      function astSectionName(state, chars, phraseIndex, phraseLength, data) {
        const ret = id.SEM_OK;
        if (state === id.SEM_PRE) {
          const name = apgLib.utils.charsToString(chars, phraseIndex, phraseLength);
          currentSection = findSection(name, data);
          if (currentSection === undefined) {
            currentSection = {};
            data[name] = currentSection;
          }
        }
        return ret;
      }
  • §

    Push a value into the current key object.

      function astValue(state, chars, phraseIndex, phraseLength) {
        const ret = id.SEM_OK;
        if (state === id.SEM_PRE) {
          const value = apgLib.utils.charsToString(chars, phraseIndex, phraseLength);
          currentKey.push(value);
        }
        return ret;
      }
  • §

    Define all of the callback functions that will be used.

      this.callbacks = [];
      this.callbacks.alpha = false;
      this.callbacks.alphadigit = false;
      this.callbacks.any = false;
      this.callbacks.badblankline = false;
      this.callbacks.badsectionline = false;
      this.callbacks.badvalueline = false;
      this.callbacks.blankline = false;
      this.callbacks.comment = false;
      this.callbacks.digit = false;
      this.callbacks.dquotedstring = false;
      this.callbacks.goodblankline = false;
      this.callbacks.goodsectionline = false;
      this.callbacks.goodvalueline = false;
      this.callbacks.inifile = astIniFile;
      this.callbacks.keyname = astKeyName;
      this.callbacks.lineend = false;
      this.callbacks.section = false;
      this.callbacks.sectionline = false;
      this.callbacks.sectionname = astSectionName;
      this.callbacks.squotedstring = false;
      this.callbacks.value = astValue;
      this.callbacks.valuearray = false;
      this.callbacks.valueline = false;
      this.callbacks.wsp = false;
    };